
#include "x_test.h"

#include <stdbool.h>
#include <stdint.h>
#include "cli_shell.h"
#include "systick.h"

#define DBG_TAG "XT"
#define DBG_LVL DBG_LOG
#include <dbg_print.h>


void timer_delay_init(void)
{

    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER2);

    timer_deinit(TIMER2);

    /* TIMER configuration */
    timer_initpara.prescaler         = 11;//@120MHz,=10MHz
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_DOWN;
    timer_initpara.period            = 65535;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER2,&timer_initpara);

    /* auto-reload preload enable */
    //timer_enable(TIMER2);
    timer_single_pulse_mode_config(TIMER2, TIMER_SP_MODE_SINGLE);
}
void timer_udelay(uint16_t us)
{
#if 1
    timer_counter_value_config(TIMER2, us*10-6);
    timer_enable(TIMER2);
    timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
    while (!timer_flag_get(TIMER2, TIMER_INT_FLAG_UP))
        ;
#else
    TIMER_CNT(TIMER2) = (uint32_t)us;
    TIMER_CTL0(TIMER2) |= (uint32_t)TIMER_CTL0_CEN;
    TIMER_INTF(TIMER2) = (~(uint32_t)TIMER_INT_FLAG_UP);
    while (!(TIMER_INTF(TIMER2) & TIMER_INT_FLAG_UP))
        ;
#endif
}
void x_test_init(void)
{
    /*vs_ctl */
    /* enable the led clock */
    rcu_periph_clock_enable(RCU_GPIOA);
    /* configure led GPIO port */ 
    gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_4);
    GPIO_BOP(GPIOA) = GPIO_PIN_4;//set
    //GPIO_BC(GPIOA) = GPIO_PIN_4;//clear
}

static int move_x(int argc, char **argv)
{
    volatile uint32_t time;
    uint32_t cycle;
    uint32_t count;
    uint8_t dir;
    if (argc > 3)
    {
        dir = atoi(argv[1]);
        cycle = atoi(argv[2]);
        count = atoi(argv[3]);
        timer_delay_init();

        /** STALL_X */
        /* enable the led clock */
        rcu_periph_clock_enable(RCU_GPIOB);
        /* configure led GPIO port */ 
        gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ,GPIO_PIN_11);

        /** EN_X */
        /* enable the led clock */
        rcu_periph_clock_enable(RCU_GPIOA);
        /* configure led GPIO port */ 
        gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
        GPIO_BOP(GPIOA) = GPIO_PIN_8;//set
        time = systick_get();
        while (systick_get() - time < 5);
        GPIO_BC(GPIOA) = GPIO_PIN_8;//clear


        /** STEP_X */
        /* enable the led clock */
        rcu_periph_clock_enable(RCU_GPIOB);
        /* configure led GPIO port */ 
        gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
        GPIO_BC(GPIOB) = GPIO_PIN_10;//clear

        /**DIR_X */
        /* enable the led clock */
        //rcu_periph_clock_enable(RCU_GPIOB);
        /* configure led GPIO port */ 
        gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
        if (dir)
        {
            GPIO_BOP(GPIOB) = GPIO_PIN_9;//set
        }
        else 
        {
            GPIO_BC(GPIOB) = GPIO_PIN_9;//clear
        }
        LOG_D("move x start:dir=%d,count=%d", dir, count);
        while(count--)
        {
            GPIO_BOP(GPIOB) = GPIO_PIN_10;//set
            //time = systick_get();
            //while (systick_get() - time < 1);
            timer_udelay(cycle/2);
            GPIO_BC(GPIOB) = GPIO_PIN_10;//clear
            timer_udelay(cycle/2);
            //time = systick_get();
            //while (systick_get() - time < 1);
        }
        LOG_D("move x end");
        GPIO_BOP(GPIOA) = GPIO_PIN_8;//set
    }
    else {
        LOG_D("Usage: \n");
        LOG_D("move_x [dir] [cycle:us] [count]");
    }

    return 0;
}
CLI_CMD_EXPORT(move_x, move_x [dir] [cycle] [count]);
